package org.yray.sip.visit;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.yray.sip.base.MsgType;
import org.yray.sip.base.SipMsg;
import org.yray.sip.constant.SessionStatusEnum;
import org.yray.sip.constant.SipCodeEnum;
import org.yray.sip.creator.MessageCreator;
import org.yray.sip.factory.StopWatchFactory;
import org.yray.sip.factory.impl.ActionByeA;
import org.yray.sip.rtp.receive.AbstractBeanHolder;
import org.yray.sip.rtp.sender.ISipSessionCache;
import org.yray.sip.server.SipServerContext;
import org.yray.sip.server.SipSession;
import org.yray.sip.utils.IcspBeanHolder;


import java.util.Optional;


@Slf4j
public class SipByeVisitor extends AbstractBeanHolder implements ISipVisitor{

    @Override
    public void doALeg(SipMsg sipMsg, SipServerContext ssc) {
        log(sipMsg.getCallId(),"Sip200Visitor,doALeg");
        SipSession sipSession= IcspBeanHolder.getBean(ISipSessionCache.class).get(sipMsg.getCallId());
        //客户方先挂的情况，跟座席方回个Bye
        if (sipSession.getToManId()==null&&!StringUtils.isEmpty(sipSession.getToManWaitId()))
        {
            log.info("客户方先挂机，分机没有接起来的情况，CallId={}",sipSession.getCallId());
            SipSession toManWaitSipSession = IcspBeanHolder.getBean(ISipSessionCache.class).get(sipSession.getToManWaitId());
            Optional.ofNullable(toManWaitSipSession).ifPresent(v -> {
                String msg = MessageCreator.getCreator(MsgType.BYE).createMsg(v);
                getSipServerHandler(sipSession.getCallId()).sendToSipServer(sipSession,msg);
                //这里发通知dialogen了，因为是客户方先取消的，所这里应该是要通知了
                sipSession.getStatus().set((SessionStatusEnum.CLOSE));

            });
        }
        //分机有接起来的情况
        else {
            log.info("客户方先挂机，分机接起来的情况，CallId={}",sipSession.getCallId());
            SipSession toManSipSession = IcspBeanHolder.getBean(ISipSessionCache.class).get(sipSession.getToManId());
            Optional.ofNullable(toManSipSession).ifPresent(v -> {
                v.getStatus().set(SessionStatusEnum.CLOSE);
                String msg = MessageCreator.getCreator(MsgType.BYE).createMsg(v);
                getSipServerHandler(toManSipSession.getCallId()).sendToSipServer(sipSession,msg);
            });
        }
        sipSession.getStatus().set(SessionStatusEnum.CLOSE);
        IcspBeanHolder.getBean(StopWatchFactory.class).periodStart(sipSession,SipCodeEnum.SIP_BYE_A.getDisplayName());
        getEventFactory().sipEvent(sipSession.getCallId(), SipCodeEnum.SIP_BYE_A);
    }

    @Override
    public void doBLeg(SipMsg sipMsg, SipServerContext ssc) {
        log(sipMsg.getCallId(),"SipByeVisitor,doBLeg");
        //这里证明是有转人工时成功的，并且是座席先挂的情况，那就向客户方回个Bye
        SipSession sipSession=IcspBeanHolder.getBean(ISipSessionCache.class).get(sipMsg.getCallId());
        sipSession.getStatus().set(SessionStatusEnum.CLOSE);
        IcspBeanHolder.getBean(ActionByeA.class).action(sipMsg);
        String aId=getIdRuleComponent().getAIdFromAOrBId(sipMsg.getCallId());
        SipSession aSipSession=IcspBeanHolder.getBean(ISipSessionCache.class).get(aId);
        IcspBeanHolder.getBean(StopWatchFactory.class).periodStart(aSipSession,SipCodeEnum.SIP_BYE_B.getDisplayName());
        getEventFactory().sipEvent(sipSession.getCallId(), SipCodeEnum.SIP_BYE_B);
    }
}
